import environment.World;
import gui.TestComponent;
import gui.TestFrame;

import java.awt.Toolkit;
import java.util.Timer;

public class SwarmingPreySimulation
{	
	public static void main(String[] args)
	{		
		// initiate World
		int width = 1000;
		int height = 800;
		World world = new World(width, height);
		
		// create GUI
		TestFrame frame = new TestFrame(width + 10, height + 20);
		frame.add(new TestComponent(world));
		
		// start simulation
		gameLoop2(frame, world);
	}
	
	public static void simpleLoop(TestFrame frame, World world)
	{
		int counter = 0;
		
		while(true)
		{
//			System.out.println("---------------- Round " + counter++ + " -----------------");
			world.update();
			frame.getComponent(0).repaint();
			
			try{
				Thread.sleep(20);
//				Thread.sleep(500);
			} catch(InterruptedException exception) {}
		}
	}
	
	public static void gameLoop2(TestFrame frame, World world)
	{
		long ticksPerSecond = 24;
		long skipTicks = 1000 / ticksPerSecond;
		int maxFrameSkip = 10;
		
		long nextGameTick = System.currentTimeMillis();
		int loops;
		
		boolean gameIsRunning = true;
		int counter = 0;
		while (gameIsRunning)
		{
			loops = 0;
			while (System.currentTimeMillis() > nextGameTick && loops < maxFrameSkip)
			{
				world.update();
				nextGameTick += skipTicks;
				loops++;
			}
			
			frame.getComponent(0).repaint();
		}
	}
	
	public static void gameLoop1(TestFrame frame, World world)
	{
		// initiate game loop
		long prev_frame_tick = 0;
		long curr_frame_tick = System.currentTimeMillis();
				
		boolean game_is_running = true;
		while (game_is_running)
		{
			prev_frame_tick = curr_frame_tick;
			curr_frame_tick = System.currentTimeMillis();
					
			//world.update(curr_frame_tick - prev_frame_tick);
			frame.getComponent(0).repaint();
		}
	}
}
